﻿<Router AppAssembly="typeof(App).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <div class="login-container">
                    <LoadingBackground ShowLogoBox="true">
                        <div class="login-body">
                            <label>Sorry, You're not authorized to reach this page. You may need to log in as a different user.</label><br />
                            <MatButton Link="/" Raised="true">Home</MatButton>
                        </div>
                    </LoadingBackground>
                </div>
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>
    <NotFound>
        <CascadingAuthenticationState>
            <LayoutView Layout="typeof(MainLayout)">
                <LoadingBackground ShowLogoBox="true">
                    <div class="login-body">
                        <h1>We lost this page</h1>
                        <label>We searched high and low but couldn't find what you're looking for.<br />Don't worry our junior developer is working on it between his naps and <a href="https://www.youtube.com/watch?v=y7LAbdoNBJA" target="_blank">youtube videos</a> on Blazor!</label><br />
                        <MatButton Link="/" Raised="true">Return Home</MatButton>
                    </div>
                </LoadingBackground>
            </LayoutView>
        </CascadingAuthenticationState>
    </NotFound>
</Router>
<MatToastContainer />

@code
{
    [Inject] HttpClient _httpClient { get; set; }
    [Inject] NavigationManager _navigationManager { get; set; }

#if ServerSideBlazor
    [Inject] IHttpContextAccessor _http { get; set; }
#endif

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        _httpClient.BaseAddress = new Uri(_navigationManager.BaseUri);

#if ServerSideBlazor
        // likely user refreshed page, we need to grab the cookies if they exist and pass it to the HttpClient for this request
        if (_http != null && _http.HttpContext != null && _http.HttpContext.Request.Cookies.Any())
        {
            var cks = new List<string>();

            foreach (var cookie in _http.HttpContext.Request.Cookies)
            {
                cks.Add($"{cookie.Key}={cookie.Value}");
            }

            _httpClient.DefaultRequestHeaders.Add("Cookie", String.Join(';', cks));
        }
#endif
    }
}